<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ECharts</title>
    <!-- 引入 echarts.js -->
<!--    <script type="text/javascript" src="https://registry.npmmirror.com/echarts/5.5.0/files/dist/echarts.min.js"></script>-->
<!--    <script src="https://cdn.staticfile.net/jquery/1.10.2/jquery.min.js"></script>-->
    <script type="text/javascript" src="/echarts/echarts.min.js"></script>
    <script type="text/javascript" src="/jquery/jquery-3.1.1.min.js"></script>
</head>
<body>
<!-- 为ECharts准备一个具备大小（宽高）的Dom -->
<div id="main" style="width: 600px;height:400px;"></div>
<script type="text/javascript">
    // 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById('main'));
    function  init(myChart) {
        // 指定图表的配置项和数据
        var option = {
            title: {
                text: 'ECharts 入门示例'
            },
            tooltip: {},
            legend: {
                data:['销量']
            },
            xAxis: {
                data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
            },
            yAxis: {},
            series: [{
                name: '销量',
                type: 'pie',
                data: [5, 20, 36, 10, 10, 20]
            }]
        };

        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
    }

    //异步访问后端，从数据库读取数据传递给echarts
    function show(myChart){
        $.ajax({
            contentType:"application/json",
            type:"GET",
            url:"/goods/getGoodsList",
            dataType:"json",
            success:function (data) {
                console.log(data)
                //定义数组
                let xdata=[]
                let sdata =[]
                for (let i = 0;i<data.length;i++){
                    xdata[i]=data[i].name
                    sdata[i]=data[i].num
                }
                myChart.setOption({
                    xAxis: {
                        data:xdata
                    },
                    series: {
                        data:sdata
                    },
                    title: {
                        text:"商品库存"
                    }

                })


            }
        })
    }

    init(myChart)
    show(myChart)


</script>
</body>
</html>